package com.gitee.hljdrl.dialog.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.gitee.hljdrl.dialog.R;
import com.gitee.hljdrl.dialog.basis.OnItemClickListener;
import com.gitee.hljdrl.dialog.entity.SheetEntity;

import java.util.List;

public class SheetAdapter extends RecyclerView.Adapter<SheetAdapter.SheetHolder> {

    protected Context mContext;
    protected List<SheetEntity> mList;
    protected LayoutInflater mLayoutInflater;
    protected OnItemClickListener<SheetEntity> mOnItemClickListener;
    protected int mFirstDrawable = 0;
    protected int mFirstLineVisible = View.VISIBLE;

    public SheetAdapter(Context context, List<SheetEntity> list) {
        this.mContext = context;
        this.mList = list;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setFirstDrawable(@DrawableRes int firstDrawable) {
        this.mFirstDrawable = firstDrawable;
    }

    public void setFirstLine(int visibility) {
        this.mFirstLineVisible = visibility;
    }

    @NonNull
    @Override
    public SheetHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View mView = mLayoutInflater.inflate(R.layout.dialog_sheet_adapter_item, null);
        return new SheetHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull SheetHolder holder, @SuppressLint("RecyclerView") int position) {
        int itemRes = R.drawable.dialog_sheet_item_selector;
        if(position==0 && mFirstDrawable!=0){
            itemRes = mFirstDrawable;
        }
        if (position == 0) {
            holder.v_line.setVisibility(mFirstLineVisible);
        }
        holder.itemView.setBackgroundResource(itemRes);
        final SheetEntity date = mList.get(position);
        holder.tv_name.setText(date.getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(position, date);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    public void setOnItemClickListener(OnItemClickListener<SheetEntity> mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public static class SheetHolder extends RecyclerView.ViewHolder {
        private final TextView tv_name;
        private final View v_line;

        public SheetHolder(View itemView) {
            super(itemView);
            this.tv_name = itemView.findViewById(R.id.tv_sheet);
            this.v_line = itemView.findViewById(R.id.v_line);
        }
    }
}
